#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHM_SIZE 1024




int main()
{
    key_t key=ftok(".",'A');
    if(key<0)
    {
        perror("ftok failed");
        exit(EXIT_FAILURE);
    }
    int shmid=shmget(key,SHM_SIZE,0666);
    if(shmid<0)
    {
        perror("shmget failed");
        exit(EXIT_FAILURE);
    }
    char* shmaddr=(char*)shmat(shmid,NULL,0);
    if(shmaddr==(char*)-1)
    {
        perror("shmat failed");
        exit(EXIT_FAILURE);
    }
    // 4.使用共享内存
   printf("I read from shared memory: %s\n",shmaddr);
    // 5.解除映射
    if(shmdt(shmaddr)<0)
    {
        perror("shmdt failed");
        exit(EXIT_FAILURE);
    }
    
    return 0;
}